package com.tsd.core.constants;

import cn.hutool.db.Entity;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName: VersionConstants
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2024/4/22 14:00
 * @Version: 1.0
 */
public class VersionConstants {

    public static final String VERSION_CACHE_KEY = "tsd";
    /**
     * 模块版本：free-免费版、basic-基础版、pro-专业版，ultimate-旗舰版
     */
    public static final String MODULE_VERSION_FREE = "free";
    public static final String MODULE_VERSION_BASIC = "basic";
    public static final String MODULE_VERSION_PRO = "pro";
    public static final String MODULE_VERSION_ULTIMATE = "ultimate";
    /**
     * 模块版本取值：0-免费版、1-基础版、2-专业版，3-旗舰版
     */
    public static final int VERSION_VALUE_FREE = 0;
    public static final int VERSION_VALUE_BASIC = 1;
    public static final int VERSION_VALUE_PRO = 2;
    public static final int VERSION_VALUE_ULTIMATE = 3;

    private static final Map<String, Integer> VERSION_NAME_VALUE;
    private static final Map<String, String> VERSION_NAME_VALUE_CN;
    private static final Map<Integer, String> VERSION_NAME_VALUE_CN_VALUE;

    static {
        VERSION_NAME_VALUE = new ConcurrentHashMap<>();
        VERSION_NAME_VALUE.put(MODULE_VERSION_ULTIMATE, VERSION_VALUE_ULTIMATE);
        VERSION_NAME_VALUE.put(MODULE_VERSION_PRO, VERSION_VALUE_PRO);
        VERSION_NAME_VALUE.put(MODULE_VERSION_BASIC, VERSION_VALUE_BASIC);
        VERSION_NAME_VALUE.put(MODULE_VERSION_FREE, VERSION_VALUE_FREE);
        VERSION_NAME_VALUE_CN = new ConcurrentHashMap<>();
        VERSION_NAME_VALUE_CN.put(MODULE_VERSION_ULTIMATE, "旗舰版");
        VERSION_NAME_VALUE_CN.put(MODULE_VERSION_PRO, "专业版");
        VERSION_NAME_VALUE_CN.put(MODULE_VERSION_BASIC, "基础版");
        VERSION_NAME_VALUE_CN.put(MODULE_VERSION_FREE, "免费版");
        VERSION_NAME_VALUE_CN_VALUE = new ConcurrentHashMap<>();
        VERSION_NAME_VALUE_CN_VALUE.put(VERSION_VALUE_ULTIMATE, "旗舰版");
        VERSION_NAME_VALUE_CN_VALUE.put(VERSION_VALUE_PRO, "专业版");
        VERSION_NAME_VALUE_CN_VALUE.put(VERSION_VALUE_BASIC, "基础版");
        VERSION_NAME_VALUE_CN_VALUE.put(VERSION_VALUE_FREE, "免费版");
    }
    /**
     * 获取当前账套版本
     *
     * @return
     */
    public static String getAccountSetVersion() {
        Entity entity = DataSourceContextHolder.getAccountSet();
        if (entity == null || !entity.containsKey("version")) {
            return VersionConstants.MODULE_VERSION_FREE;
        }
        String version = entity.getStr("version");
        if (HlpUtils.isEmpty(version)) {
            return VersionConstants.MODULE_VERSION_FREE;
        }
        return version;
    }

    public static boolean accountSetVersionIsFree(){
        return VersionConstants.MODULE_VERSION_FREE.equals(getAccountSetVersion());
    }
    public static Integer getVersionValue(String name) {
        return VERSION_NAME_VALUE.get(name);
    }

    public static String getVersionName(String name) {
        return VERSION_NAME_VALUE_CN.get(name);
    }

    public static Map<Integer, String> getVersionValueMap() {
        return VERSION_NAME_VALUE_CN_VALUE;
    }
}
